package com.augurit.common.syslog.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.augurit.common.dao.SysLoginLogDao;
import com.augurit.common.entity.SysLoginLog;
import com.augurit.common.syslog.convert.SysLoginLogConvertor;
import com.augurit.common.syslog.service.ISysLoginLogService;
import com.augurit.common.syslog.web.form.SysLoginLogForm;
import com.augurit.common.util.HqlUtils;
import com.augurit.common.util.PageUtils;
import com.augurit.common.util.QueryDateFieldUtils;

@Service
@Transactional
public class SysLoginLogServiceImpl implements ISysLoginLogService {

	@Autowired
	private SysLoginLogDao sysLoginLogDao;
	
	/**
	 * 保存新增或修改的Form对象
	 */
	public void save(SysLoginLogForm form){
		
		if(form != null){
			SysLoginLog entity = null;
			
			//准备VO对象
			if(form != null && form.getId() != null){
				entity = sysLoginLogDao.get(form.getId());
			}else{
				entity = new SysLoginLog();
			}
			
			//属性值转换
			SysLoginLogConvertor.convertFormToVo(form, entity);
			
			//保存
			sysLoginLogDao.save(entity);
			
			//回填ID属性值
			form.setId(entity.getId());
		}
	}
	
	/**
	 * 删除form对象
	 */
	public void delete(Long... ids) {
		sysLoginLogDao.delete(ids);
	}

	/**
	 * 获取form对象
	 */
	public SysLoginLogForm get(Long id) {
		SysLoginLogForm form = null;
		if(id != null){
			SysLoginLog entity = sysLoginLogDao.get(id);
			form = SysLoginLogConvertor.convertVoToForm(entity);
		}
		
		return form;
	}

	/**
	 * 新增或修改form对象
	 * @param forms
	 */
	public void save(SysLoginLogForm... forms) {
		if(forms != null)
			for(SysLoginLogForm form : forms)
				this.save(form);
	}
	

	/**
	 * 根据Form对象的查询条件作分页查询
	 *@param page page对象，包含分页参数
	 *@param form form对象，包含查询条件
	 *@return page对象
	 */
	@Transactional(readOnly = true)
	public Page<SysLoginLogForm> search(Page<SysLoginLogForm> page, SysLoginLogForm form) {
		// 建立新的分页对象
		Page pg = PageUtils.newPageInstance(page);
		
		// 查询语句及参数
		StringBuffer hql = new StringBuffer("from SysLoginLog ps where 1=1");
		List values = new ArrayList();
		
		// 查询条件
		if(form != null){
			if(form.getUserName() != null && form.getUserName().trim().length() > 0){
				hql.append(" and ps.userName like ?");
				values.add("%" + form.getUserName() + "%");
			}
			if(form.getLoginState() != null && form.getLoginState().trim().length() > 0){
				hql.append(" and ps.loginState = ?");
				values.add(form.getLoginState());
			}
			if(form.getDaysFromNow() != 0 && form.getDaysFromNow() != QueryDateFieldUtils.ALL_DAYS){
				hql.append(" and ps.loginTime between ? and ?");
				values.add(QueryDateFieldUtils.getDateFromNow(form.getDaysFromNow()));
				values.add(new Date());
			}
			
		}
		
		//排序
		hql.append(HqlUtils.buildOrderBy(page, "ps"));
		
		// 执行分页查询操作
		sysLoginLogDao.findPage(pg, hql.toString(),values);
		// 转换为Form对象列表并赋值到原分页对象中
		List<SysLoginLogForm> list = SysLoginLogConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}

	
	
	public Page<SysLoginLogForm> search(Page<SysLoginLogForm> page, List<PropertyFilter> filters) {
		return null;
	}

}
